﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADO.NET.Samples.Common;
using ADO.NET.Samples.DataSets.DBDataSetTableAdapters;
using ADO.NET.Samples.DataSets;

namespace ADO.NET.Samples.Forms.LINQ
{
    [FormDescription("LINQ to Typed DataSet Sample", "Demonstrate how to use LINQ to query data from typed DataSet.")]
    public partial class LINQtoTypedDataSetSample : Form
    {
        public LINQtoTypedDataSetSample()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Load data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LINQtoTypedDataSetSample_Load(object sender, EventArgs e)
        {
            DBDataSet ds = new DBDataSet();
            ArticleTableAdapter articleAdapter = new ArticleTableAdapter();
            UserTableAdapter userAdapater = new UserTableAdapter();
            articleAdapter.Fill(ds.Article);
            userAdapater.FillUsers(ds.User);
            var articles = from article in ds.Article
                           join user in ds.User
                           on article.UserID equals user.UserID
                           let Author = user.UserName
                           where user.UserID < 8
                           select new
                           {
                               Author,
                               article.ArticleTitle,
                               article.ArticleContent
                           };
            foreach (var article in articles)
            {
                this.listBox1.Items.Add(string.Format("{0}(Author: {1})", article.ArticleTitle, article.Author));
            }
        }
    }
}
